/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import java.sql.SQLException;
import java.util.List;
import weibo4j.Timeline;
import weibo4j.Weibo;
import weibo4j.model.Paging;
import weibo4j.model.Source;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;
import SQL.SQL;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import weibo4j.model.User;

/**
 *
 * @author Administrator
 */
public class getTimeline {

    public static void main(String[] args) throws WeiboException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        prepareweibo();
        get(Config.uid, 27635);
    }

    public static void prepareweibo() {
        String accessToken = Auth.Auth4Code.getCode();
        Weibo weibo = new Weibo();
        weibo.setToken(accessToken);
    }

    public static void get(String uid, int j) throws WeiboException, ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        Timeline tm = new Timeline();
        int i = 1;
        Date ct = Calendar.getInstance().getTime();
        ct.setDate(ct.getDate() - 1);
        while (true) {
            i++; List<Status> status=new ArrayList<Status>();
            try {
                status= tm.getHomeTimeline(0, 0, new Paging(i, 200));
                
            } catch (Exception e) {
                while(status==null){
                    status= tm.getHomeTimeline(0, 0, new Paging(i, 200));
                }
                
            }
            String table = "wb_demo_status";
            for (Status s : status) {
                Date createTime = s.getCreatedAt();
                if (createTime.before(ct)) {
                    break;
                }
                String userid = Config.uid;
                j++;
                String urid = "0";
                User u = s.getUser();
                if (u != null) {
                    urid = u.getId();
                }
                String mid = s.getMid();
                String id = s.getId();
                String sourcename = s.getSource().getName();
                String text = s.getText();
                int repostCount = s.getRepostsCount();
                int commentCount = s.getCommentsCount();
                String bmiddle_pic = s.getBmiddlePic();
                String original_pic = s.getOriginalPic();
                String thumbnail_pic = s.getThumbnailPic();
                String[] columns = {"no", "mid", "uid", "fid", "id", "text", "source", "bmiddle_pic", "original_pic", "thumbnail_pic", "reposts_count", "comments_count", "createAt"};
                String[] values = {j + "", mid, userid, urid, id, text, sourcename, bmiddle_pic, original_pic, thumbnail_pic, repostCount + "", commentCount + "", createTime.toString()};
                try {
                    System.out.println(j + " " + createTime.toString());
                    SQL.SQL(SQL.insert(columns, values, table));
                } catch (Exception e) {
                }
                if (s.getRetweetedStatus() != null) {

                    userid = "0";

                    Status r = s.getRetweetedStatus();
                    urid = "0";
                    u = (User) r.getUser();
                    if (u != null) {
                        urid = u.getId();
                    }
                    String retweetID = r.getMid();
                    String[] tempC = {"retweetID"};
                    String[] tempV = {retweetID};
                    SQL.SQL(SQL.updateValue(tempC, tempV, table, "mid", mid));
                    mid = r.getMid();
                    id = r.getId();
                    createTime = r.getCreatedAt();
                    if (createTime == null) {
                        createTime = ct;
                    }
                    sourcename = r.getSource().getName();
                    text = r.getText();
                    repostCount = r.getRepostsCount();
                    commentCount = r.getCommentsCount();
                    bmiddle_pic = r.getBmiddlePic();
                    original_pic = r.getOriginalPic();
                    thumbnail_pic = r.getThumbnailPic();
                    String[] select = {"mid"};
                    String[] v = {mid};
                    Boolean a = SQL.Query(select, select, v, table);
                    if (a) {
                        continue;
                    } else {
                        j++;
                        String[] rcolumns = {"no", "mid", "uid", "fid", "id", "text", "source", "bmiddle_pic", "original_pic", "thumbnail_pic", "reposts_count", "comments_count", "createAt"};
                        String[] rvalues = {j + "", mid, userid, urid, id, text, sourcename, bmiddle_pic, original_pic, thumbnail_pic, repostCount + "", commentCount + "", createTime.toString()};
                        try {
                            System.out.println(j);
                            SQL.SQL(SQL.insert(rcolumns, rvalues, table));
                        } catch (Exception e) {
                        }
                    }
                }

            }

        }
    }
}
